#include <iostream>
#include <vector>
using namespace std;
// 下标从1开始便于计算
 // l：f*2 \
    r: father*2+1 \
    father: child/2
vector<int> heap;
int e = 1;
void up(int i)
{
    while (i > 1)
    {
        int p = i / 2;
        if (heap[p] <= heap[i])
            break;
        swap(heap[p], heap[i]);
        i = p;
    }
}
void down(int i)
{
    while (i < e)
    {
        int l = i * 2 < e ? i * 2 : 0;
        int r = i * 2 + 1 < e ? i * 2 + 1 : 0;
        int lower = 0;
        if (!l && !r)
            break;
        if (!l)
            lower = r;
        else if (!r)
            lower = l;
        else
            lower = heap[l] > heap[r] ? r : l;
        if (heap[lower] >= heap[i])
            break;
        swap(heap[lower], heap[i]);
        i = lower;
    }
}
void ins(int x)
{
    heap[e++] = x;
    up(e - 1);
}
// 删除任意位置
void rm(int i)
{
    swap(heap[i], heap[e-- - 1]);
    up(i), down(i);
}
int m, n;
int main()
{
    cin >> m >> n;
    heap.resize(m + 1);
    for (e = 1; e <= m; e++)
    {
        int temp;
        cin >> temp;
        // n * logn 建堆
        // ins(temp);
        heap[e] = temp;
    }
    // O(n) 建堆
    for (int i = (e - 1) / 2; i; i--)
    {
        down(i);
    }
    while (n--)
    {
        cout << heap[1] << ' ';
        rm(1);
    }

    return 0;
}
//类
#include<iostream>
#include<vector>
using namespace std;
template<class T>
class MyPriorityQueue{
public:
    MyPriorityQueue(vector<T>&&v):_vt(v){
        init_heap();
    }
    MyPriorityQueue(vector<T>&v):_vt(v){
        init_heap();
    }
    MyPriorityQueue():_vt(){;}
    void push(const T& x){
        _vt.push_back(forward(x));
        up(_vt.size()-1);
    }
    void pop(){
        swap(_vt.front(),_vt.back());
        _vt.pop_back();
        down(0);
    }
    const T& top(){
        return _vt.front();
    }
private:
    void init_heap(){
        for(int i=(_vt.size()-2)/2;i>=0;i--){
            down(i);
        }
    };
    void down(int i){
        int l=i;
        if(i*2+1<_vt.size()&&_vt[i*2+1]<_vt[l])l=i*2+1;
        if(i*2+2<_vt.size()&&_vt[i*2+2]<_vt[l])l=i*2+2;
        if(l!=i){
            swap(_vt[i],_vt[l]);
            down(l);
        }
    }
    void up(int i){
        int p=(i-1)/2;
        if(p>=0&&_vt[p]>_vt[i]){
            swap(_vt[p],_vt[i]);
            up(p);
        }
    }
private:
    vector<T>_vt;
};
int main(){
    int n,m;
    cin>>n>>m;
    vector<int>vt(n,0);
    for(int i=0;i<n;i++)
        cin>>vt[i];
    MyPriorityQueue heap(std::move(vt));
    while(m--){
        cout<<heap.top()<<' ';
        heap.pop();
    }
    return 0;
}